package org.hp.Servlet;

import com.alibaba.fastjson.JSON;
import org.hp.service.AService;
import org.hp.service.impl.AServiceImpl;
import org.hp.util.Layui;
import org.hp.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/LogisticsServlet")
public class LogisticsServlet extends HttpServlet {
    AService as=new AServiceImpl();
    Layui layui=new Layui();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if(method.equals("queryAll")){
            queryAll(request,response);
        }else if (method.equals("queryOne")){
            queryOne(request,response);
        }else if(method.equals("update")){
            update(request,response);
        }else if(method.equals("delete")){
            delete(request,response);
        }else if(method.equals("insert")){
            insert(request,response);
        }
    }
    private void insert(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String loname=request.getParameter("loname");
            String loidcard=request.getParameter("loidcard");
            String lotel=request.getParameter("lotel");
            Date lotime=sdf.parse(request.getParameter("lotime"));
            if (as.renow("insert into logistics values(null,?,?,?,?)",loname,loidcard,lotel,lotime)){
                response.getWriter().write(JSON.toJSONString(layui.update(0)));
            }else{
                response.getWriter().write(JSON.toJSONString(layui.update(1)));
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }

    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if(as.renow("delete from logistics where loid=?",Integer.valueOf(request.getParameter("id")))){
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        }else{
            response.getWriter().write(JSON.toJSONString(layui.update(1)));
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            int loid=Integer.valueOf(request.getParameter("loid"));
            String loname=request.getParameter("loname");
            String loidcard=request.getParameter("loidcard");
            String lotel=request.getParameter("lotel");
            Date lotime=sdf.parse(request.getParameter("lotime"));
            if (as.renow("update logistics set loname=?,loidcard=?,lotel=?,lotime=? where loid=?",loname,loidcard,lotel,lotime,loid)){
                response.getWriter().write(JSON.toJSONString(layui.update(0)));
            }else{
                response.getWriter().write(JSON.toJSONString(layui.update(1)));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    private void queryOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Map> list=as.query("select * from logistics where loid=?",Integer.valueOf(request.getParameter("id")));
        System.out.println("queryOne:"+list);
        response.getWriter().write(JSON.toJSONString(layui.select(list.size(),list)));
    }

    private void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer page=request.getParameter("page")!=null?Integer.valueOf(request.getParameter("page")):1;
        Integer limit=request.getParameter("limit")!=null?Integer.valueOf(request.getParameter("limit")):10;
        String loname= request.getParameter("loname")!=null?"%" + request.getParameter("loname") + "%":"%%";
        String sql="select * from logistics where loname like '"+ loname +"'";
        PageBean<Map> list=as.pageFind(page,limit,sql,loname);
        response.getWriter().write(JSON.toJSONString(layui.select(list)));
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
